package com.open.capacity.reptile.task;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.open.capacity.finance.task.ScheduledOfTask;
import com.open.capacity.reptile.constant.FinanceConstant;
import com.open.capacity.reptile.dto.common.ResponseWithCookies;
import com.open.capacity.reptile.dto.finance.request.BillDataRequest;
import com.open.capacity.reptile.dto.finance.request.XszDataRequest;
import com.open.capacity.reptile.dto.finance.response.*;
import com.open.capacity.reptile.service.SourcePurchaseNoticeService;
import com.open.capacity.reptile.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Component
@Slf4j
public class FinanceBillDataTask implements ScheduledOfTask {

    @Autowired
    private SourcePurchaseNoticeService service;

    Integer cronId = null;

    @Override
    public void setCronId(Integer cronId) {
        cronId = cronId;
    }


    @Override
    public void execute() throws UnsupportedEncodingException {

    }

    @Override
    public void trigger(String startTime) throws IOException {

    }


    //TODO 调试版，待删除
    public static void main(String[] args) throws IOException {

        //1. 校验密码
        ResponseWithCookies response = HttpUtils.requestForGet(FinanceConstant.pwd_url, null);

        String storecode = HttpUtils.getCookieValue(response.getCookies(), "storecode");
        String SESSIONID2 = HttpUtils.getCookieValue(response.getCookies(), "SESSIONID2");
        String LTPATOKEN = HttpUtils.getCookieValue(response.getCookies(), "LTPATOKEN");
        String dgd_wwlid = HttpUtils.getCookieValue(response.getCookies(), "dgd_wwlid");
        String gatewaytoken = HttpUtils.getCookieValue(response.getCookies(), "gateway-token");

        //2. sso_login单点登录
        HttpUtils.requestForPost(MessageFormat.format(FinanceConstant.sso_login_url, storecode, SESSIONID2, LTPATOKEN, dgd_wwlid), new HashMap<>(), response.getCookies());


        Header[] headers = new Header[4];
        headers[0] = new BasicHeader("annual", "2023");
        headers[1] = new BasicHeader("orgCode", "440100000_Agency_217001");
        headers[2] = new BasicHeader("postCode", "440100000-666003");
        headers[3] = new BasicHeader("Authorization", "Bearer " + gatewaytoken);

        HashMap param = new HashMap();
        param.put("featuresName", "业务单据记账");
        param.put("appCode", "440100_dwkjhs_guangzhou_2023");
        param.put("functionsType", "0");
        param.put("menuCode", "4ea17607-9a92-4e2e-a408-c95b6b18af6b_AGLA_HESUAN4_100101001004@1055608570307154056");

        //3. 获取token
        ResponseWithCookies requestTokenStr = HttpUtils.requestForPost(FinanceConstant.gettoken_url, param, response.getCookies(), headers);
        RoleDataResponse requestToken = JSONObject.parseObject(requestTokenStr.getContent(), new TypeReference<RoleDataResponse>() {
        });

        //4. 获取tokenid
        ResponseWithCookies responseTokenidStr = HttpUtils.requestForGet(MessageFormat.format(FinanceConstant.initTokenid_url, requestToken.getData().getToken()), response.getCookies());

        String tokenid = HttpUtils.getCookieValue(responseTokenidStr.getCookies(), "tokenid");

        BillDataRequest billDataRequest = new BillDataRequest();
        billDataRequest.setAgencyCode("217001");
        billDataRequest.setSchemeGuid("a15cb6b5-26b4-4247-a233-77f84caaffbc");
        billDataRequest.setDateFrom("2023-06-01");
        billDataRequest.setDateTo("2023-06-30");
        billDataRequest.setBillNo("");
        billDataRequest.setBillState("-1");
        billDataRequest.setField(new String[0]);
        billDataRequest.setAcctCode("101");
        billDataRequest.setRgCode("440100000");
        billDataRequest.setSetYear("2023");
        billDataRequest.setCurrentPage("1");
        billDataRequest.setPageSize("100");


        List<Cookie> cookieList = new ArrayList<>();
        BasicClientCookie item = new BasicClientCookie("storecode", storecode);
        item.setDomain("ctglqscwg.gdczt.gov.cn");
        cookieList.add(item);

        item = new BasicClientCookie("dgd_wwlid", LTPATOKEN);
        item.setDomain("ctglqscwg.gdczt.gov.cn");
        cookieList.add(item);

        item = new BasicClientCookie("LTPATOKEN", LTPATOKEN);
        item.setDomain("ctglqscwg.gdczt.gov.cn");
        cookieList.add(item);

        item = new BasicClientCookie("tokenid", tokenid);
        item.setDomain("ctglqscwg.gdczt.gov.cn");
        cookieList.add(item);


        //5. 获取业务记账单数据
        ResponseWithCookies responseBilldataStr = HttpUtils.requestForJson(MessageFormat.format(FinanceConstant.getBussinessBillDataUrl, requestToken.getData().getRoledata().get(0).getRoleId()), JSONObject.toJSONString(billDataRequest), cookieList);
        CommonResponse<BillData> responseBilldata = JSONObject.parseObject(responseBilldataStr.getContent(), new TypeReference<CommonResponse<BillData>>() {
        });


        //6. 获取科目期初余额
        ResponseWithCookies responseInitialdataStr = HttpUtils.requestForJson(MessageFormat.format(FinanceConstant.getInitialUrl, requestToken.getData().getRoledata().get(0).getRoleId()), "{\"agencyCode\":\"217001\",\"acctCode\":\"101\",\"accsCode\":\"001\",\"setYear\":2023,\"rgCode\":\"440100000\",\"isShowLastBal\":0,\"isDoubleVou\":0}", cookieList);
        CommonResponse<InitialData> responseInitialdata = JSONObject.parseObject(responseInitialdataStr.getContent(), new TypeReference<CommonResponse<InitialData>>() {
        });


        //7. 获取序时账

        XszDataRequest xszDataRequest = new XszDataRequest();
        xszDataRequest.setAgencyCode("217001");

        ResponseWithCookies responseXszdataStr = HttpUtils.requestForJson(MessageFormat.format(FinanceConstant.xszUrl, requestToken.getData().getRoledata().get(0).getRoleId()), "{\"acctCode\":\"101\",\"agencyCode\":\"217001\",\"acctName\":\"广州市花都区人民法院\",\"agencyName\":\"广州市花都区人民法院\",\"prjCode\":\"\",\"prjName\":\"\",\"prjScope\":\"\",\"prjGuid\":\"\",\"rptType\":\"GL_RPT_CHRBOOK\",\"setYear\":2023,\"userId\":\"831333171630182400\",\"prjContent\":{\"agencyAcctInfo\":[{\"acctCode\":\"101\",\"agencyCode\":\"217001\"}],\"startDate\":\"2023-01-01\",\"endDate\":\"2023-12-31\",\"startYear\":2023,\"startFisperd\":\"\",\"endYear\":2023,\"endFisperd\":\"\",\"period\":\"dateBn\",\"qryItems\":[{\"itemType\":\"ACCO\",\"itemIndex\":1,\"itemTypeName\":\"会计科目\",\"items\":[],\"isShowItem\":\"0\",\"isGradsum\":\"0\",\"isCascade\":true,\"itemLevel\":\"\"},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"ACCO\",\"itemTypeName\":\"会计科目\",\"items\":[],\"seq\":1},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"EXPECO\",\"itemTypeName\":\"部门支出经济分类\",\"items\":[],\"seq\":2},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"ACC_ITEM02\",\"itemTypeName\":\"财政指标\",\"items\":[],\"seq\":3},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"COSTTYPE\",\"itemTypeName\":\"费用经济性质\",\"items\":[],\"seq\":4},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"CURRENT\",\"itemTypeName\":\"往来单位\",\"items\":[],\"seq\":5},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"PROJECT\",\"itemTypeName\":\"项目\",\"items\":[],\"seq\":6},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"DEPPRO\",\"itemTypeName\":\"预算项目\",\"items\":[],\"seq\":7},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"EXPFUNC\",\"itemTypeName\":\"支出功能分类科目\",\"items\":[],\"seq\":8},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"FATYPE\",\"itemTypeName\":\"资产分类\",\"items\":[],\"seq\":9},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"PAYTYPE\",\"itemTypeName\":\"支付方式\",\"items\":[],\"seq\":10},{\"isGradsum\":\"0\",\"isShowItem\":\"1\",\"itemDir\":\"\",\"itemLevel\":\"-1\",\"itemPos\":\"condition\",\"itemType\":\"FUNDTYPE\",\"itemTypeName\":\"资金性质\",\"items\":[],\"seq\":11}],\"rptCondItem\":[],\"rptOption\":[{\"defCompoValue\":\"Y\",\"optCode\":\"IS_VOUSTAT_UNAUDIT\",\"optName\":\"未审核\"},{\"defCompoValue\":\"Y\",\"optCode\":\"IS_VOUSTAT_AUDITED\",\"optName\":\"已审核\"},{\"defCompoValue\":\"Y\",\"optCode\":\"IS_VOUSTAT_POSTED\",\"optName\":\"已记账\"},{\"defCompoValue\":\"Y\",\"optCode\":\"IS_VOUSTAT_CANCELED\",\"optName\":\"已作废\"},{\"defCompoValue\":\"Y\",\"optCode\":\"IS_INCLUDE_YJ\",\"optName\":\"含月结凭证\"},{\"defCompoValue\":\"Y\",\"optCode\":\"IS_INCLUDE_NJ\",\"optName\":\"含年结凭证\"}],\"curCode\":\"\",\"rptStyle\":\"\",\"formCode\":\"\",\"formName\":\"\",\"rptTitleName\":\"\",\"pageLength\":10,\"currPage\":1,\"rowNumber\":100}}", cookieList);
        CommonResponse<XszData> responseXszdata = JSONObject.parseObject(responseXszdataStr.getContent(), new TypeReference<CommonResponse<XszData>>() {
        });
    }
}

